﻿#region Using directives

using System;
using System.Text;

#endregion

namespace AspNetForums.WebServices
{
	[Serializable]
	public class ExpressEntry
	{
		public ExpressEntry()
		{
		}

		public Project Project;
		public string Body;
		public string Subject;
		public byte[] ScreenShot;
	}

	[Serializable]
    public class Project
    {
        #region Constructors

        public Project()
        {
        }

        #endregion

        #region Methods
        public void Save(string directory)
        {
            string filePath;
            System.IO.FileStream fs;

            foreach (ProjectFile file in projectFiles)
            {
                filePath = System.IO.Path.Combine(directory, file.RelativePath);
                if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filePath)))
                {
                    System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filePath));
                }
                fs = System.IO.File.Create(filePath);
                fs.Write(file.Contents, 0, file.Contents.Length);
                fs.Close();
            }
        }

		public void SaveCompressed( string fileName )
		{
			Xceed.Zip.Licenser.LicenseKey = "ZIN20-NF649-5H2S1-BKCA";
			Xceed.Zip.ZipArchive archive = new Xceed.Zip.ZipArchive( new Xceed.FileSystem.DiskFile( fileName ) );
			archive.BeginUpdate();
			foreach (ProjectFile file in projectFiles)
			{
				Xceed.Zip.ZippedFile zipFile = ( Xceed.Zip.ZippedFile )archive.GetFile( file.RelativePath );
				if( !zipFile.Exists )
					zipFile.Create();

				using( System.IO.Stream stream = zipFile.OpenWrite( true ) )
				{
					stream.Write( file.Contents, 0, file.Contents.Length );
				}
			}
			archive.EndUpdate();
		}
        #endregion
        

        public ProjectFile[] ProjectFiles
        {
            get { return projectFiles; }
            set { projectFiles = value; }
        } private ProjectFile[] projectFiles;

        public string Description
        {
            get { return description; }
            set { description = value; }
        } private string description;

        public string Title
        {
            get { return title; }
            set { title = value; }
        } private string title;

        [System.Xml.Serialization.XmlIgnore]
        public System.Drawing.Image ScreenShot
        {
            get { return screenShot; }
            set 
            {
                this.screenShotBytes = null;
                screenShot = value;
            }
        } private System.Drawing.Image screenShot;

        private byte[] screenShotBytes;
        public byte[] ScreenShotBytes
        {
            get 
            {
                if (screenShot == null) return null;
                if (screenShotBytes != null) return screenShotBytes;
                
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                screenShot.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                ms.Position = 0;
                screenShotBytes = new byte[ms.Length];
                if (ms.Length < Int32.MaxValue)
                    ms.Read(screenShotBytes, 0, (int)ms.Length);
                else
                    throw (new ArgumentException("Too many bytes dude", "ScreenShotBytes"));

                return screenShotBytes; 
            }
            set
            {
                screenShotBytes = value;
                System.IO.MemoryStream ms = new System.IO.MemoryStream(value);
                screenShot = System.Drawing.Image.FromStream(ms);
            }
        }


    }

    [Serializable]
    public struct ProjectFile
    {
        public ProjectFile(string relativePath, byte[] contents)
        {
            RelativePath = relativePath;
            Contents = contents;
        }
        public string RelativePath;
        public byte[] Contents;
    }
}
